package nj.yb.mapper;

import nj.yb.entity.user.Friend;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface FriendMapper {
    //添加好友
    void addFriend(
            @Param("userId") Integer userId,
            @Param("friendId") Integer friendId
    );

    //通过用户id查找该用户的所有好友
    @Select("SELECT f.id, f.user_id, f.friend_id, f.create_time, " +
            "u.id AS friend_info_id, u.username AS friend_info_username, u.nickname AS friend_info_nickname, u.create_time AS friend_info_create_time " +
            "FROM friend f " +
            "JOIN user u ON f.friend_id = u.id " +
            "WHERE f.user_id = #{userId}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "friend_id", property = "friendId"),
            @Result(column = "create_time", property = "createTime"),
            // 映射 friendInfo 的字段
            @Result(column = "friend_info_id", property = "friendInfo.id"),
            @Result(column = "friend_info_username", property = "friendInfo.username"),
            @Result(column = "friend_info_nickname", property = "friendInfo.nickname"),
            @Result(column = "friend_info_create_time", property = "friendInfo.createTime")
    })
    List<Friend> findFriendsByUserId(Integer userId);


    @Select("SELECT f.id, f.user_id, f.friend_id, f.create_time, " +
            "u.id AS friend_info_id, u.username AS friend_info_username, u.nickname AS friend_info_nickname, u.create_time AS friend_info_create_time " +
            "FROM friend f " +
            "JOIN user u ON f.friend_id = u.id " +
            "WHERE f.user_id = #{userId} AND f.friend_id = #{friendId}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "friend_id", property = "friendId"),
            @Result(column = "create_time", property = "createTime"),
            // 映射 friendInfo 的字段
            @Result(column = "friend_info_id", property = "friendInfo.id"),
            @Result(column = "friend_info_username", property = "friendInfo.username"),
            @Result(column = "friend_info_nickname", property = "friendInfo.nickname"),
            @Result(column = "friend_info_create_time", property = "friendInfo.createTime")
    })
    Friend check(
            @Param("userId") Integer userId,
            @Param(("friendId")) Integer friendId);
}
